﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Model.Orders;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.OrderService;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class OrderStatusesController : ControllerBase
    {
        #region Index
        //
        // GET: /OrderStatuses/

        public ActionResult Index()
        {
            IEnumerable<OrderStatus> orderStatuses = null;
            try
            {
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderStatuses = orderService.GetOrderStatusList();
                orderService.Close();
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderStatusList, MessageType.Error);
            }
            return View(orderStatuses);
        } 
        #endregion

        #region Create
        //
        // GET: /OrderStatuses/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /OrderStatuses/Create

        [HttpPost]
        public ActionResult Create(OrderStatus status)
        {
            try
            {
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderService.StoreOrderStatus(status);
                orderService.Close();
                this.SetMessage(InfoStrings.OrderStatusAdded, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderStatusAdding, MessageType.Error);

            }
            return View(status);
        } 
        #endregion

        #region Edit
        //
        // GET: /OrderStatuses/Edit/5

        public ActionResult Edit(int id)
        {
            OrderServiceClient orderService = new OrderServiceClient();
            orderService.AddValidationToken(this.Token);
            OrderStatus status = orderService.GetOrderStatus(id);
            orderService.Close();
            if (status == null)
            {
                this.SetMessage(ErrorStrings.ErrorOrderStatus, MessageType.Error);
                return RedirectToAction("Index");
            }
            return View(status);
        }

        //
        // POST: /OrderStatuses/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, OrderStatus status)
        {
            try
            {
                status.Id = id;
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderService.StoreOrderStatus(status);
                orderService.Close();
                this.SetMessage(InfoStrings.OrderStatusEdited, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderStatusEditing, MessageType.Error);

            }
            return View(status);
        } 
        #endregion

        #region Delete
        //
        // GET: /OrderStatuses/Delete/5

        public ActionResult Delete(int id)
        {
            return Edit(id);
        }

        //
        // POST: /OrderStatuses/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderService.DeleteOrderStatus(id);
                orderService.Close();
                this.SetMessage(InfoStrings.OrderStatusDeleted, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderStatusDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
